﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Net;
using System.Text.RegularExpressions;
using System.IO.Compression;

namespace RukouMVC.Helpers
{
    public class CookieHelpers
    {
        public static void saveState(string key, string value, bool remember, bool logout)
        {
            //string _value = FransferChar(value);
            string _value = value;

            HttpCookie MyCookie = HttpContext.Current.Request.Cookies["rukoumvc" + key];
            if (MyCookie != null)
            {
                MyCookie.Value = HttpContext.Current.Server.HtmlEncode(value);
            }
            else
            {
                MyCookie = new HttpCookie("rukoumvc" + key);
                MyCookie.Value = HttpContext.Current.Server.HtmlEncode(value);

            }

            if (remember)
                MyCookie.Expires = DateTime.Now.AddYears(1);
            if (logout)
                MyCookie.Expires = DateTime.Now.AddYears(-1);

            HttpContext.Current.Response.Cookies.Add(MyCookie);
            HttpContext.Current.Items[key] = _value;
        }

        public static string getState(string key)
        {

            if (HttpContext.Current.Items[key] != null)
            {
                //return UnFransferChar(HttpContext.Current.Items[key].ToString());
                return HttpContext.Current.Items[key].ToString();
            }
            HttpCookie MyCookie;

            if (HttpContext.Current.Request.Cookies["rukoumvc" + key] != null)
            {
                MyCookie = HttpContext.Current.Request.Cookies["rukoumvc" + key];
                //return UnFransferChar(HttpContext.Current.Server.HtmlDecode(MyCookie.Value));
                return HttpContext.Current.Server.HtmlDecode(MyCookie.Value);
            }
            else
                return "";
        }

        public string this[string key]
        {
            get
            {
                string result = CookieHelpers.getState(key);
                return result;
            }

        }

        public string this[string key, bool remember, bool logout]
        {
            set
            {
                CookieHelpers.saveState(key, value, remember, logout);
            }
        }


        public static string FransferChar(string s)
        {
            string _encrpts = OtherHelpers.EncryptData("rukou", s);
            //return _encrpts;
            return _encrpts.Replace("<", "*L*E*F*T*").Replace(">", "*R*I*G*H*T*").Replace("\\", "*F*E*E*T*").Replace(";", "*S*E*M*I*C*O*L*O*N*").Replace("&", "*A*N*D*");
        }

        public static string UnFransferChar(string s)
        {

            string _sfordecrpt = s.Replace("*L*E*F*T*", "<").Replace("*R*I*G*H*T*", ">").Replace("*F*E*E*T*", "\\").Replace("*S*E*M*I*C*O*L*O*N*", ";").Replace("*A*N*D*", "&");
            _sfordecrpt = OtherHelpers.DecryptData("rukou", _sfordecrpt);
            return _sfordecrpt;
        }


    }
}
